﻿@page "/publish/course"
@inject CourseService CourseService
@inject MessageService Message
@inject ModalService ModalService;
@inject ConfirmService ComfirmService;
@inject NavigationManager Nav
@using AntDesign.TableModels

<AntDesign.PageHeader Title="课程管理">

</AntDesign.PageHeader>
<AntDesign.Button Type="ButtonType.Primary" OnClick="ShowCreateModal">
    <Icon Type="plus" /> 新增课程
</AntDesign.Button>
<Input
@bind-Value="searchText"
Placeholder="请输入课程名称" 
OnKeyDown="HandleSearch" 
Style="width: 300px; margin-left: 20px;" />

<Table TItem="Course"
DataSource="filteredCourses"
Total="totalCourses"
OnChange="OnTableChange"
PageSize="8"
@ref="_table"
Bordered
Size="TableSize.Middle">
    <ColumnDefinitions Context="course">
        <PropertyColumn Title="课程名称"
        Property="@(c => c.CourseName)"
        Sortable
        Filterable>
            <Template>
                <b>@course.CourseName.ToUpper()</b>
            </Template>
        </PropertyColumn>
        <PropertyColumn Title="课程描述" Property="@(c => c.Introduction)" />
        <PropertyColumn Title="跳转链接" Property="@(c => c.Redirection)" />
        <PropertyColumn Title="图片地址" Property="@(c => c.ImageUrl)" />
        <ActionColumn Title="操作" Width="200px">
            <Space>
                <AntDesign.Button Type="ButtonType.Link"
                OnClick="@(() => ShowEditModal(course))">
                    <Icon Type="edit" /> 编辑
                </AntDesign.Button>
                <Popconfirm Title="确认删除？"
                OnConfirm="@(() => DeleteCourse(course))">
                    <AntDesign.Button Type="ButtonType.Link" Danger>
                        <Icon Type="delete" /> 删除
                    </AntDesign.Button>
                </Popconfirm>
                <AntDesign.Button Type="ButtonType.Primary" OnClick="@(() => NavigateToChapters(course))" Disabled="@(course?.Id <= 0)">
                    章节管理
                </AntDesign.Button>
            </Space>
        </ActionColumn>
    </ColumnDefinitions>
</Table>


<AntDesign.Modal @bind-Visible="modalVisible" 
Title="@(isEditMode ? "编辑课程" : "新增课程")"
OnOk="SubmitForm">
    <AntDesign.Form Model="@currentCourse">
        @if (isEditMode)
        {
            <AntDesign.FormItem Label="ID" Rules="@(new[] { new FormValidationRule { Required = true, Message = "ID不能为空" } })">
                <AntDesign.Input @bind-Value="currentCourse.Id" ReadOnly />
            </AntDesign.FormItem>
        }
        <AntDesign.FormItem Label="课程名称" Rules="@(new[] { new FormValidationRule { Required = true, Message = "名称不能为空" } })">
            <AntDesign.Input @bind-Value="currentCourse.CourseName" />
        </AntDesign.FormItem>
        <AntDesign.FormItem Label="课程描述">
            <TextArea @bind-Value="currentCourse.Introduction" />
        </AntDesign.FormItem>
        <AntDesign.FormItem Label="跳转链接">
            <AntDesign.Input @bind-Value="currentCourse.Redirection" />
        </AntDesign.FormItem>
        <AntDesign.FormItem Label="图片地址">
            <AntDesign.Input @bind-Value="currentCourse.ImageUrl" />
        </AntDesign.FormItem>
    </AntDesign.Form>
</AntDesign.Modal>


@code {
    private ITable _table;
    private List<Course> courses = new();
    private List<Course> filteredCourses = new();
    IEnumerable<Course> _selectedRows = [];
    private Course currentCourse = new();
    private string searchText = "";
    private int totalCourses;
    private int currentPage = 1;
    private bool modalVisible;
    private bool isEditMode;

    protected override async Task OnInitializedAsync()
    {
        await LoadData();
    }

    private async Task LoadData()
    {

        courses = await CourseService.GetCoursesAsync();
        totalCourses = courses.Count;
        _table?.ReloadData();
    }

    public void NavigateToChapters(Course course)
    {
        if (course?.Id > 0)
        {
            
            Nav.NavigateTo($"/publish/course/{course.Id}");
        }
        else
        {
            Message.Error("课程ID无效");
        }
    }

    // 表格变化处理
    private void OnTableChange(QueryModel<Course> query)  // 需要AntDesign.TableModels命名空间 [[4]]
    {
        var queryable = courses.AsQueryable()
            .Where(c => c.CourseName.Contains(searchText, StringComparison.OrdinalIgnoreCase));

        filteredCourses = queryable
            .ExecuteTableQuery(query)  // 使用AntDesign的查询执行方法 [[6]]
            .CurrentPagedRecords(query)
            .ToList();
    }

    private void HandleSearch(KeyboardEventArgs args)
    {
        if (args.Key == "Enter")
        {
            currentPage = 1;
            _table?.ReloadData();
        }
    }

    private void ShowCreateModal()
    {
        currentCourse = new Course();
        isEditMode = false;
        modalVisible = true;
    }

    private void ShowEditModal(Course course)
    {
        currentCourse = course;
        isEditMode = true;
        modalVisible = true;
    }

    private async Task SubmitForm()
    {
        if (isEditMode)
        {
            //await CourseService.UpdateCourseAsync(currentCourse);
        }
        else
        {
            //await CourseService.CreateCourseAsync(currentCourse);
        }
        modalVisible = false;
        await LoadData();
        await Message.Success(isEditMode ? "更新成功" : "新增成功");
    }

    private async Task DeleteCourse(Course course)
    {
        //await CourseService.DeleteCourseAsync(course.Id);
        await LoadData();
        await Message.Success("删除成功");
    }
    
}
